package com.goktech.olala.client.controller.customer;

import com.goktech.olala.core.resp.RespCollectVo;
import com.goktech.olala.core.service.ICollectService;
import com.goktech.olala.core.service.IRegisterService;
import com.goktech.olala.server.pojo.customer.Customer;
import com.goktech.olala.server.pojo.customer.Login;
import com.goktech.olala.server.pojo.goods.Collect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Classname AccountController
 * @Description TODO
 * @Date 2020/12/10 9:28
 * @Created by sanming
 */

/**
 * 注册
 */
@RestController
@RequestMapping("/cntApi")
public class AccountController {
    @Autowired
    IRegisterService iRegisterService;

    @Autowired
    ICollectService iCollectService;

    /**
     * 邮箱注册
     * @param password
     * @param request
     * @return
     */
    @RequestMapping("/register.do")
    @ResponseBody
    public String register(String password,HttpServletRequest request){

        String email = request.getParameter("email");
        String customerId = String.valueOf(System.currentTimeMillis());
        Login login = new Login();
        login.setCustomerId(customerId);
        login.setLoginName(email);
        login.setPassword(password);
        int insert = iRegisterService.insert(login);
        String s = String.valueOf(insert);
        return s;
    }
    /**
     * 登入
     */

    @RequestMapping("/login.do")
    @ResponseBody
    public String login(String userName ,String password,HttpServletRequest request){
        Login login = iRegisterService.selectUserAndPassword(userName, password);
        if (login == null){
            return "10000";
        }
        String loginName = login.getLoginName();
        String customerId = login.getCustomerId();
        request.getSession().setAttribute("customerId",customerId);
        request.getSession().setAttribute("LoginUserName",loginName);
        return "-10000";
    }

    /**
     * 查询个人信息
     * @param request
     * @return
     */
    @RequestMapping( "/info.do")
    @ResponseBody
    public ModelAndView info(HttpServletRequest request){
        ModelAndView mav = new ModelAndView();
        String loginUserName = (String) request.getSession().getAttribute("LoginUserName");
        if (loginUserName == null){
            mav.setViewName("home/login");
            return mav;
        }
        Customer customer = iRegisterService.selectEmail(loginUserName);
        mav.addObject("LoginUserInfo",customer);
        request.getSession().setAttribute("SessionLoginUserInfo",customer);
        mav.setViewName("person/information");
        return mav;
    }

    /**
     * 个人信息修改
     */
    @RequestMapping( "/updateUserInfo.do")
    @ResponseBody
    public ModelAndView updateInfo(String ctm_name,String r_name,Integer radio10,HttpServletRequest request){
    ModelAndView mav = new ModelAndView();
        String userEmail = request.getParameter("user-email");
        String userPhone = request.getParameter("user-phone");
        String LoginUserName = (String) request.getSession().getAttribute("LoginUserName");
        Customer email = iRegisterService.selectEmail(LoginUserName);
        Long customerInfId = email.getCustomerInfId();
        Customer customer = new Customer();
        customer.setCustomerName(ctm_name);
        customer.setRealName(r_name);
        customer.setGender(radio10);
        customer.setEmail(userEmail);
        customer.setUserMobile(userPhone);
        customer.setCustomerInfId(customerInfId);
        iRegisterService.updateByPrimaryKey(customer);
        iRegisterService.updateEmail(customer.getEmail(),LoginUserName);
        request.getSession().setAttribute("LoginUserName",customer.getEmail());
        mav.setViewName("redirect:/cntApi/info.do");
        return mav;
    }

    /**
     * 收藏
     * @param request
     * @return
     */
    @RequestMapping(value = "/myCollection.do")
    @ResponseBody
    public ModelAndView myCollection(HttpServletRequest request){
        ModelAndView view = new ModelAndView();
        Collect collect = new Collect();
        String customerId = (String) request.getSession().getAttribute("customerId");
        collect.setCustomerId(customerId);

        List<RespCollectVo> collectVos = iCollectService.queryAllMyCollect(collect);
        view.addObject("collectVos",collectVos);
        view.setViewName("person/collection");
        return view;
    }

    /**
     * 商品咨询
     *
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/consultation.do",method = RequestMethod.POST)
    @ResponseBody
    public ModelAndView consultation(){
        ModelAndView view = new ModelAndView();
        view.setViewName("person/consultation");
        return view;
    }

    /**
     * 退出登入
     */
    @RequestMapping( "/logout.do")
    @ResponseBody
    public String loginout(HttpServletRequest request){
        request.getSession().removeAttribute("LoginUserName");
        request.getSession().removeAttribute("LoginUserId");


    return "10000";
    }
}

